package com.concurrent.data.thread.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author 哲思小辉
 * @version 1.0.0
 * @ClassName RedisCofig.java
 * @Description redis配置类
 * @Param
 * @createTime 2020年03月27日 10:22:00
 */
@Configuration
public class RedisCofig {


    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //使用fastjson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // 1、key的序列化采用MyStringRedisSerializer（加了前缀）
        template.setKeySerializer(new MyStringRedisSerializer());
        template.setHashKeySerializer(new MyStringRedisSerializer());
        ///2、key的序列化采用StringRedisSerializer （没有前缀）
//        template.setKeySerializer(new StringRedisSerializer());
//        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
